#include "System_Devices.h"

/*********************************TaskHandle_t******************************************/
TaskHandle_t Key_Handle;
TaskHandle_t Determine_Handle;
TaskHandle_t Screen_Handle;
TaskHandle_t Apple_Handle;
TaskHandle_t State_Handle;
/*********************************Devices_Task******************************************/
void Devices_Key(void *arg);
void Devices_Determine(void *arg);
void Devices_Screen(void *arg);
void Devices_Apple(void *arg);
void Devices_Snake_State(void *arg);
/***************************************************************************************/
void Service_Devices_Init(void){
	xTaskCreate(Devices_Key, 		  "Dev.Key",  	    Normal_Stack_Size,    	NULL, PriorityHigh,        &Key_Handle);
	xTaskCreate(Devices_Determine,"Dev.Determine",  Normal_Stack_Size,    	NULL, PriorityHigh,        &Determine_Handle);
	xTaskCreate(Devices_Screen,   "Dev.Screen",     Normal_Stack_Size,    	NULL, PriorityHigh,        &Screen_Handle);
	xTaskCreate(Devices_Apple,    "Dev.Apple",      Normal_Stack_Size,    	NULL, PriorityHigh,        &Apple_Handle);
	xTaskCreate(Devices_Snake_State,    "Dev.State",      Normal_Stack_Size,    	NULL, PriorityHigh,        &State_Handle);
}


/*********************************Task**************************************************/
void Devices_Key(void *arg){
	TickType_t xLastWakeTime_t = xTaskGetTickCount();
	TickType_t _xTicksToWait = pdMS_TO_TICKS(1);
	for(;;){
			if(BaiZe_KeyState == 0 && Snake.state == 1){
			XPT2046_Get_TouchedPoint ( &BaiZe, &strXPT2046_TouchPara[Lcd_Scan_Mode] , Lcd_Scan_Mode );	
			while(Get_XPT2046_State()){;}
			BaiZe_KeyState = 1;
		}
		vTaskDelayUntil(&xLastWakeTime_t, _xTicksToWait);
	}
}


void Devices_Determine(void *arg){
	TickType_t xLastWakeTime_t = xTaskGetTickCount();
	TickType_t _xTicksToWait = pdMS_TO_TICKS(500);
	for(;;){
		if(BaiZe_KeyState == 1 && Snake.state == 1){
			if((80<BaiZe.x && BaiZe.x<160) && (200<BaiZe.y && BaiZe.y<260) ){
				if(Snake.direction != 3)
					Snake.direction = 1;
			}
			if((0<BaiZe.x && BaiZe.x<80) && (230<BaiZe.y && BaiZe.y<290) ){
				if(Snake.direction != 4)
					Snake.direction = 2;
			}
			if((80<BaiZe.x && BaiZe.x<160) && (260<BaiZe.y && BaiZe.y<320) ){
				if(Snake.direction != 1)
					Snake.direction = 3;
			}
			if((160<BaiZe.x && BaiZe.x<240) && (230<BaiZe.y && BaiZe.y<290) ){
				if(Snake.direction != 2)
					Snake.direction = 4;
			}
			BaiZe_KeyState = 0;
		}
		vTaskDelayUntil(&xLastWakeTime_t, _xTicksToWait);
	}
}


void Devices_Screen(void *arg){
	TickType_t xLastWakeTime_t = xTaskGetTickCount();
	TickType_t _xTicksToWait = pdMS_TO_TICKS(500);
	for(;;){
		
		
		if(Snake.direction == 1 && Snake.state == 1){
			for(uint16_t i = 0 ; i<Snake.lenth-1 ; i++){
				LCD_DrowDot( Snake.body[i].x, Snake.body[i].y,13 , BLACK);
				Snake.body[i].x = Snake.body[i+1].x;
				Snake.body[i].y = Snake.body[i+1].y;
			}
			Snake.body[Snake.lenth-1].x = Snake.body[Snake.lenth-1].x;
			Snake.body[Snake.lenth-1].y = Snake.body[Snake.lenth-1].y-13;
			for(uint16_t i = 0;i<Snake.lenth;i++){
				LCD_DrowDot( Snake.body[i].x, Snake.body[i].y , 13 , WHITE);
			}
			if(LCD_Read_OnePoint(Snake.body[Snake.lenth-1].x , Snake.body[Snake.lenth-1].y-13) == 0xFFFF){
				Snake.lenth++;
				Snake.body[Snake.lenth-1].x = Snake.body[Snake.lenth-2].x;
				Snake.body[Snake.lenth-1].y = Snake.body[Snake.lenth-2].y-13;
				LCD_DrowDot( Snake.body[Snake.lenth-1].x, Snake.body[Snake.lenth-1].y,13 , WHITE);
				Apple_state = 0;
			}
		}
		
		
		
		if(Snake.direction == 2 && Snake.state == 1){
			for(uint16_t i = 0 ; i<Snake.lenth-1 ; i++){
				LCD_DrowDot( Snake.body[i].x, Snake.body[i].y,13 , BLACK);
				Snake.body[i].x = Snake.body[i+1].x;
				Snake.body[i].y = Snake.body[i+1].y;
			}
			Snake.body[Snake.lenth-1].x = Snake.body[Snake.lenth-1].x-13;
			Snake.body[Snake.lenth-1].y = Snake.body[Snake.lenth-1].y;
			for(uint16_t i = 0;i<Snake.lenth;i++){
				LCD_DrowDot( Snake.body[i].x, Snake.body[i].y,13 , WHITE);
			}
			if(LCD_Read_OnePoint(Snake.body[Snake.lenth-1].x-13 , Snake.body[Snake.lenth-1].y) == 0xFFFF){
				Snake.lenth++;
				Snake.body[Snake.lenth-1].x = Snake.body[Snake.lenth-2].x-13;
				Snake.body[Snake.lenth-1].y = Snake.body[Snake.lenth-2].y;
				LCD_DrowDot( Snake.body[Snake.lenth-1].x, Snake.body[Snake.lenth-1].y,13 , WHITE);
				Apple_state = 0;
			}
		}
		
		
		if(Snake.direction == 3 && Snake.state == 1){
			for(uint16_t i = 0 ; i<Snake.lenth-1 ; i++){
				LCD_DrowDot( Snake.body[i].x, Snake.body[i].y,13 , BLACK);
				Snake.body[i].x = Snake.body[i+1].x;
				Snake.body[i].y = Snake.body[i+1].y;
			}
			Snake.body[Snake.lenth-1].x = Snake.body[Snake.lenth-1].x;
			Snake.body[Snake.lenth-1].y = Snake.body[Snake.lenth-1].y+13;
			for(uint16_t i = 0;i<Snake.lenth;i++){
				LCD_DrowDot( Snake.body[i].x, Snake.body[i].y , 13 , WHITE);
			}
			if(LCD_Read_OnePoint(Snake.body[Snake.lenth-1].x , Snake.body[Snake.lenth-1].y+13) == 0xFFFF){
				Snake.lenth++;
				Snake.body[Snake.lenth-1].x = Snake.body[Snake.lenth-2].x;
				Snake.body[Snake.lenth-1].y = Snake.body[Snake.lenth-2].y+13;
				LCD_DrowDot( Snake.body[Snake.lenth-1].x, Snake.body[Snake.lenth-1].y,13 , WHITE);
				Apple_state = 0;
			}
		}
		
		
		
		if(Snake.direction == 4 && Snake.state == 1){
			for(uint16_t i = 0 ; i<Snake.lenth-1 ; i++){
				LCD_DrowDot( Snake.body[i].x, Snake.body[i].y,13 , BLACK);
				Snake.body[i].x = Snake.body[i+1].x;
				Snake.body[i].y = Snake.body[i+1].y;
			}
			Snake.body[Snake.lenth-1].x = Snake.body[Snake.lenth-1].x+13;
			Snake.body[Snake.lenth-1].y = Snake.body[Snake.lenth-1].y;
			for(uint16_t i = 0;i<Snake.lenth;i++){
				LCD_DrowDot( Snake.body[i].x, Snake.body[i].y,13 , WHITE);
			}
			if(LCD_Read_OnePoint(Snake.body[Snake.lenth-1].x+13 , Snake.body[Snake.lenth-1].y) == 0xFFFF){
				Snake.lenth++;
				Snake.body[Snake.lenth-1].x = Snake.body[Snake.lenth-2].x+13;
				Snake.body[Snake.lenth-1].y = Snake.body[Snake.lenth-2].y;
				LCD_DrowDot( Snake.body[Snake.lenth-1].x, Snake.body[Snake.lenth-1].y,13 , WHITE);
				Apple_state = 0;
			}
		}

		vTaskDelayUntil(&xLastWakeTime_t, _xTicksToWait);
	}
}

void Devices_Apple(void *arg){
	TickType_t xLastWakeTime_t = xTaskGetTickCount();
	TickType_t _xTicksToWait = pdMS_TO_TICKS(1);
	uint8_t flag = 0;
	for(;;){
		if(Apple_state == 0 && Snake.state == 1){
			LCD_DrowDot( Apple[0], Apple[1],13 , BLACK);
			Apple_Pos1 = rand();
			Apple_Pos2 = rand();
			while(Apple_Pos1>14 || Apple_Pos2>14){
				Apple_Pos1 = Apple_Pos1%14;
				Apple_Pos2 = Apple_Pos2%14;
			}
			if(LCD_Read_OnePoint(Snake.body[Snake.lenth-1].x+13 , Snake.body[Snake.lenth-1].y) == 0xFFFF)
				flag = 0;
			else flag = 1;
	
			Apple[0] = 6+Apple_Pos1*13;
			Apple[1] = 6+Apple_Pos2*13;
		}
		if(flag == 1){
			LCD_DrowDot( Apple[0], Apple[1],13 , WHITE);
			Apple_state = 1;
			flag = 0;
		}
		vTaskDelayUntil(&xLastWakeTime_t, _xTicksToWait);
	}
}


void Devices_Snake_State(void *arg){
	TickType_t xLastWakeTime_t = xTaskGetTickCount();
	TickType_t _xTicksToWait = pdMS_TO_TICKS(1);
	uint8_t flag = 0;
	for(;;){
		if((0<=Snake.body[Snake.lenth-1].x && Snake.body[Snake.lenth-1].x<240) && (0<=Snake.body[Snake.lenth-1].y && Snake.body[Snake.lenth-1].y<200)){
			Snake.state = 1;
		}
		else Snake.state = 0;
		if(Snake.state == 0 && flag == 0){
			LCD_Clear(0 , 0 ,LCD_X_LENGTH , LCD_Y_LENGTH , BLACK);
			char str[]="GAME OVER";
			LCD_DisString_EN(12 , 160 , str , Font24x32 , RED , BLACK);
			flag = 1;
		}
			
		vTaskDelayUntil(&xLastWakeTime_t, _xTicksToWait);
	}
}
